package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.CgnciRiskQueryDTO;
import com.zatech.cgnci.project.entity.CgnciRiskDO;
import com.zatech.cgnci.project.mapper.CgnciRiskMapper;
import com.zatech.cgnci.project.service.CgnciRiskService;
import com.zatech.cgnci.project.vo.CgnciRiskClassVO;
import com.zatech.cgnci.project.vo.CgnciRiskSubTypeVO;
import com.zatech.cgnci.project.vo.CgnciRiskVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 险种基础表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2024-12-17
 */
@Service
public class CgnciRiskServiceImpl extends ServiceImpl<CgnciRiskMapper, CgnciRiskDO> implements CgnciRiskService {

    @Resource
    private CgnciRiskMapper riskMapper;

    @Override
    public IPage<CgnciRiskVO> pageRisk(CgnciRiskQueryDTO riskQueryDTO) {
        return riskMapper.pageRisk(riskQueryDTO.toPage(), riskQueryDTO);
    }

    @Override
    public List<CgnciRiskClassVO> listRiskClass() {
        return riskMapper.listRiskClass();
    }

    @Override
    public List<CgnciRiskSubTypeVO> listRiskSubType(String riskClassCode, String riskSubType) {
        List<CgnciRiskSubTypeVO> resultList = new ArrayList<>();
        if (VerifyUtils.isEmpty(riskClassCode)) {
            return riskMapper.listRiskSubType(riskClassCode, riskSubType);
        }
        List<String> list = Arrays.asList(riskClassCode.split(","));
        list.forEach(code -> {
            List<CgnciRiskSubTypeVO> riskSubTypeVOS = riskMapper.listRiskSubType(code, riskSubType);
            resultList.addAll(riskSubTypeVOS);
        });
        return resultList;
    }

    @Override
    public List<CgnciRiskSubTypeVO> listRiskInfoBySubTypeCode(String riskSubTypeCodes) {
        if (VerifyUtils.isEmpty(riskSubTypeCodes)) {
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "riskSubTypeCode cannot be null");
        }
        List<String> list = Arrays.asList(riskSubTypeCodes.split(","));
        List<CgnciRiskDO> riskDOList = this.list(new LambdaQueryWrapper<CgnciRiskDO>().in(CgnciRiskDO::getRiskSubTypeCode, list));
        return ConvertUtils.convert(riskDOList, CgnciRiskSubTypeVO.class);
    }

}
